import {
    _decorator,
    Component,
    CCInteger,
    RigidBody2D,
    PhysicsSystem2D,
    EventTouch,
    misc,
    Vec3,
    Vec2,
    math,
    SkeletalAnimation,
  } from "cc";
  const { ccclass, property } = _decorator;
  
  import { Target, SpeedType } from "./UIJoystick";
  import type { JoystickDataType } from "./UIJoystick";
import { SystemEvent } from "cc";
  
//   PhysicsSystem2D.instance.enable = true;
  
  @ccclass("UIJoystickTarget")
  export default class UIJoystickTarget extends Component {
    @property(SkeletalAnimation)
    anim: SkeletalAnimation = null!;

    @property({
      displayName: "刚体模式",
      tooltip: "不会立即停止",
    })
    rigidbody = false;
  
    // from joystick
    @property({
      displayName: "Move Dir",
      tooltip: "移动方向",
    })
    moveDir = new Vec3(0, 1, 0);
  
    @property({
      tooltip: "速度级别",
    })
    _speedType: SpeedType = SpeedType.STOP;
  
    // from self
    @property({
      type: CCInteger,
      tooltip: "移动速度",
    })
    _moveSpeed = 0;
  
    @property({
      type: CCInteger,
      tooltip: "停止时速度",
    })
    stopSpeed = 0;
  
    @property({
      type: CCInteger,
      tooltip: "正常速度",
    })
    normalSpeed = 100;
  
    @property({
      type: CCInteger,
      tooltip: "最快速度",
    })
    fastSpeed = 200;
  
    _body: RigidBody2D | null = null;
  
    onLoad() {
      if (this.rigidbody) {
        this._body = this.node.getComponent(RigidBody2D);
      }
      // if(this.anim){
      //   this.anim.play("idle");
      // }
      
      Target.on(SystemEvent.EventType.TOUCH_START, this.onTouchStart, this);
      Target.on(SystemEvent.EventType.TOUCH_MOVE, this.onTouchMove, this);
      Target.on(SystemEvent.EventType.TOUCH_END, this.onTouchEnd, this);
    }
    onDestroy() {
      Target.targetOff(this);
    }
  
    onTouchStart() {
      // if(this.anim){
      //   this.anim.play("run");
      // }
    }
  
    onTouchMove(event: EventTouch, data: JoystickDataType) {
      this._speedType = data.speedType;
      this.moveDir = data.moveVec;
  
      this.onSetMoveSpeed(this._speedType);
    }
  
    onTouchEnd(event: EventTouch, data: JoystickDataType) {
      this._speedType = data.speedType;
  
      this.onSetMoveSpeed(this._speedType);
      // if(this.anim){
      //   this.anim.play("idle");
      // }
    }
  
    /**
     * set moveSpeed by SpeedType
     * @param speedType
     */
    onSetMoveSpeed(speedType: SpeedType) {
      switch (speedType) {
        case SpeedType.STOP:
          this._moveSpeed = this.stopSpeed;
          break;
        case SpeedType.NORMAL:
          this._moveSpeed = this.normalSpeed;
          break;
        case SpeedType.FAST:
          this._moveSpeed = this.fastSpeed;
          break;
        default:
          break;
      }
    }
  
    /**
     * 移动
     */
    move() {
      // var q_tmp = new math.Quat();
      // var v_tmp = new Vec3(0, 1, 0);
      // v_tmp.normalize();
      
      let rad = misc.radiansToDegrees(Math.atan2(-this.moveDir.z, this.moveDir.x)) + 90;
      // this.node.angle = misc.radiansToDegrees(Math.atan2(this.moveDir.z, this.moveDir.x)) - 90;

      // var out_Q = math.Quat.rotateAround(q_tmp, math.Quat.IDENTITY, v_tmp, rad);
      // this.node.setRotation(out_Q.x, out_Q.y, out_Q.z, out_Q.w); 

      this.node.eulerAngles = new Vec3(0, rad, 0);


      // console.log("angle: ", rad, Math.atan2(this.moveDir.z, this.moveDir.x))
  
      if (this.rigidbody && this._body) {
        const moveVec = this.moveDir.clone().multiplyScalar(this._moveSpeed / 20);
        const force = new Vec2(moveVec.x, -moveVec.z);
        this._body.applyForceToCenter(force, true);
      } else {
        const oldPos = this.node.getPosition();
        const newPos = oldPos.add(
          // fps: 60
          this.moveDir.clone().multiplyScalar(this._moveSpeed / 60)
        );
        // console.log(this._moveSpeed / 60);
        this.node.setPosition(newPos);
  
        // console.log(newPos);
      }
    }
  
    update(deltaTime: number) {
      if (this._speedType !== SpeedType.STOP) {
        this.move();
      }
    }
  }
  